package com.game.main
{
        	import flash.display.MovieClip;
        	import flash.display.Sprite;
        	import flash.events.Event;
        	import flash.events.MouseEvent;
        	import flash.net.LocalConnection;
        	import flash.text.TextField;
        	import flash.text.TextFieldAutoSize;
        	
        	import com.game.raceroom.TyroRacingRoom;
        	import com.game.roomlist.RoomListCanvas;
        	import com.game.roomlist.room.RoomCanvas;
        	import com.game.shop.ShopCanvas;
        	
        	import mx.core.UIComponent;
        	import mx.managers.CursorManager;
	
	public class MainCanvasLogic
	{
			private var cursorID:int;
			
			
		public var mainCanvas:MainCanvas;
		public var comp:UIComponent = new UIComponent();
			public var shopMC:ShopMC = new  ShopMC();
			public var backMC:BackMC = new BackMC();
			public var friendMC:FriendMC = new FriendMC();
			public var setupMC:SetupMC = new SetupMC();
			public var bagMC:BagMC = new BagMC();
			public var orderMC:OrderMC = new OrderMC();
			
			private var msgSrt:Sprite = new Sprite;			
			
			private var path:String;
			public var roomCanvas:RoomCanvas;

			public var status:String="null";
		public function MainCanvasLogic(mainCanvas:MainCanvas)
		{
			this.mainCanvas=mainCanvas;
		}
		public function initMCCom(mc:MovieClip){
			  mc.gotoAndStop("static");
			  mc.addEventListener(MouseEvent.MOUSE_OVER,mouseOverMc);
			  mc.addEventListener(MouseEvent.MOUSE_OUT,mouseOutMc);
			  mc.buttonMode=true;
			  mc.useHandCursor=true;
		}
		
		
        public  function initMc():void{
        	
			  shopMC.x = 430;
			  shopMC.y = 480;
			  shopMC.addEventListener(MouseEvent.CLICK,clickMc); 
			  initMCCom(shopMC);
			  
			  bagMC.x = 487;
			  bagMC.y = 480;
			  bagMC.addEventListener(MouseEvent.CLICK,clickMc);
			  initMCCom(bagMC);

			  
			  orderMC.x = 544;
			  orderMC.y = 480;
			  orderMC.addEventListener(MouseEvent.CLICK,clickMc);
			  initMCCom(orderMC);
			  
			  friendMC.x = 601;
			  friendMC.y = 480;
			  friendMC.addEventListener(MouseEvent.CLICK,clickMc);
			  initMCCom(friendMC);
			  
			  setupMC.x = 658;
			  //setupMC.x = 601;
			  setupMC.y = 480;
			  setupMC.addEventListener(MouseEvent.CLICK,clickMc);
			  initMCCom(setupMC);
			 
			  
			  backMC.x = 715;
			  //backMC.x = 658;
			  backMC.y = 480;
			  backMC.addEventListener(MouseEvent.CLICK,clickMc); 
			  initMCCom(backMC);
			  
			  comp.addChild(shopMC); 
			  comp.addChild(friendMC); 	
			  comp.addChild(setupMC);
			  comp.addChild(backMC);	
			  comp.addChild(bagMC);
			  comp.addChild(orderMC);
		      mainCanvas.UI.addChild(comp);
            }
            public function removeFuncBtn():void{
            	comp.enabled=false;
            	comp.visible=false;
            }
            public function addFuncBtn():void{
            	comp.enabled=true;
            	comp.visible=true;
            }
            
            private function setup(e:Event):void{
            	if(status=="room"){
            		if(mainCanvas.room.readyForRace){
            			mainCanvas.popMsg("请取消准备状态后再进行操作！");
            			return;
            		}

            	}           	
            	this.mainCanvas.showSetup();
            }            
 
            private function userBag(e:Event):void{
            	if(status=="room"){
            		if(mainCanvas.room.readyForRace){
            			mainCanvas.popMsg("请取消准备状态后再进行操作！");
            			return;
            		}
            		 
            	}
            	this.mainCanvas.showUserBag();

            }
            private function orderList(e:Event):void{
            	if(status=="room"){
            		if(mainCanvas.room.readyForRace){
            			mainCanvas.popMsg("请取消准备状态后再进行操作！");
            			return;
            		}
            		 
            	}
            	this.mainCanvas.showOrderList();

            }
            private function gotoShop(e:Event):void{
            	if(status=="room"){
            		if(mainCanvas.room.readyForRace){
            			mainCanvas.popMsg("请取消准备状态后再进行操作！");
            			return;
            		}
            		
            		this.path = "room";
            		roomCanvas = mainCanvas.room;
            		//mainCanvas.applyOutofRoom();

            		mainCanvas.closePopWaiting();
            	}else
             	if(status=="roomlist"){
            		mainCanvas.outofRoomList(); 
            	}           	
            	this.mainCanvas.showShop();
            }

            private function userFriend(e:Event):void{
            	this.mainCanvas.showMyUserFriend(1);

            }            
            
            private function clickMc(evt:MouseEvent):void
            {
            	if(evt.target is BagMC)
            	{
            			userBag(evt);
            	}else if(evt.target is SetupMC)
            	{
            		setup(evt);
            	}else if(evt.target is ShopMC){
         		
            			gotoShop(evt);
            		
            	}else if(evt.target is BackMC)
            	{
            		back(evt);
            	}else if(evt.target is FriendMC)
            	{
             			userFriend(evt);
            	}  else if(evt.target is OrderMC)
            	{
            			orderList(evt);
            	}              	
            }
            
            private function mouseOverMc(evt:MouseEvent):void
            {
            	evt.target.gotoAndStop("mouseover");
	            	msgSrt.x = evt.target.x+evt.target.width/2-30;
	            	msgSrt.y = evt.target.y-20;
	            	var txt:TextField  = new TextField();
					txt.autoSize=TextFieldAutoSize.LEFT;
					txt.background = true;
					txt.selectable=false;            	
 
            	
            	if(evt.target is BagMC)
            	{
            		txt.text ="仓库";
            	}else if(evt.target is SetupMC)
            	{
            		txt.text ="设置";
            	}else if(evt.target is ShopMC){
            		txt.text ="道具店";
            		
            	}else if(evt.target is BackMC)
            	{
            		txt.text ="返回";
            	}else if(evt.target is FriendMC)
            	{
            		txt.text ="好友";
            	} else if(evt.target is OrderMC)
            	{
            		txt.text ="排行榜";
            	}              	           	
            	

            	msgSrt.addChild(txt);  
            	
            	comp.addChild(msgSrt); 
            	         	
            }
            private function mouseOutMc(evt:MouseEvent):void
            {
            	evt.target.gotoAndStop("static");
            	
            	msgSrt.removeChildAt(0);
            	comp.removeChild(msgSrt);
            	
             	
            }   
            
                  
            public function back(e:Event):void{
//  				if(mainCanvas.userInfo.newbie == 3){
// 					mainCanvas.finishGuide();
// 				} 
	        	if(status=="ppvillage"){
	        	  
	        	}else
	        	if(status=="roomlist"){
	        		mainCanvas.outofRoomList(); 
	                statusChange("ppvillage");
	        	}else
	        	if(status=="room"){
	        	 
	        	  mainCanvas.outofRoomHandler();
	        	}else
	        	if(status=="shop"){
	        	  if(path == "room"){
	        	  	statusChange("room");
	        	  	path = null;
	        	  	this.roomCanvas = null;
	        	  	GC();
	        	  }else{
	        	  	statusChange("ppvillage");
	        	  	path = null;
	        	  	
	        	  }
	        	}else
	        	if(status=="tyroroom"){
	        	 
	        	  statusChange("ppvillage");
	        	}	
            }
            public function  removeStatus(){
            	 mainCanvas.gameForm.removeAllChildren();
	        	if(status=="ppvillage"){
	        	   //mainCanvas.villageModule.destroy();
	        	}else
	        	if(status=="roomlist"){
	              //mainCanvas.roomlist.destroy();
	        	}else
	        	if(status=="room"){
	        		if(path == "room"){
	        		}else{
	        	 		mainCanvas.roomInfo.clear();
	        		}
	        	 //mainCanvas.room.destroy();
	        	}else
	        	if(status=="shop"){
	        	 
	        	  // mainCanvas.shop.destroy();
	        	}else
	        	if(status=="tyroroom"){
	        	  addFuncBtn();
			   	  mainCanvas.tyroroom.destroyGame();
	        	  // mainCanvas.shop.destroy();
	        	}else{
	        		this.path = null;
	        		this.roomCanvas = null;
	        	}	
	        	mainCanvas.villageModule=null;
	        	mainCanvas.roomlist=null;
	        	mainCanvas.room=null;
	        	mainCanvas.shop=null;
	        	mainCanvas.tyroroom=null;
	        	GC();
            }
            
            public function clearPath():void
            {
            	if(path ){
 	        		this.path = null;
	        		this.roomCanvas = null;           		
            	}
            	GC();
            }
            
			public static function GC():void 
			{ 
				try 
				{ 
					var lc1:LocalConnection = new LocalConnection(); 
					var lc2:LocalConnection = new LocalConnection(); 
					lc1.connect('name'); 
					lc2.connect('name2'); 
				} 
				catch (e:Error) 
				{ 
				} 
			} 
	        public function statusChange(status:String):void{
	        	trace("init 2");

	        	removeStatus();

	        	this.status=status;
	        	if(status=="ppvillage"){
	        	  mainCanvas.villageModule=new VillageCanvas;
	              mainCanvas.villageModule.x=0;
	              mainCanvas.villageModule.y=0;
	        	  mainCanvas.chatcanvas.alpha=0.2;
	        	  mainCanvas.adcanvas.visible=false;
	        	  mainCanvas.boardcanvas.visible=false;
	        	  mainCanvas.uicanvas.visible=false;
	        	  mainCanvas.gameForm.addChild(mainCanvas.villageModule);
	        	  mainCanvas.villageModule.mainCanvas=mainCanvas;
	        	   bagMC.visible=true;
	        	}else
	        	if(status=="roomlist"){
	        	
	               mainCanvas.roomlist=new RoomListCanvas;
	              mainCanvas.roomlist.x=1;
	              mainCanvas.roomlist.y=9;
	        	  mainCanvas.chatcanvas.alpha=1;
	        	  mainCanvas.adcanvas.visible=true;
	        	  mainCanvas.boardcanvas.visible=true;
	        	   mainCanvas.uicanvas.visible=true;
	        	  mainCanvas.gameForm.addChild(mainCanvas.roomlist);
	        	  mainCanvas.roomlist.mainCanvas=mainCanvas;
	        	  
	        	}else
	        	if(status=="room"){
	        		if(path == "room"){
	        		  //mainCanvas.roomInfo =roomCanvas.roomInfo;
 					  mainCanvas.room=roomCanvas;
 					  //mainCanvas.room.mainCanvas=mainCanvas; 
		              mainCanvas.room.x=9;
		              mainCanvas.room.y=13;	        
		              mainCanvas.uicanvas.visible=true;			
		        	  mainCanvas.gameForm.addChild(roomCanvas);
		        	  //mainCanvas.room.initRoom();
		        	  	
	        	 	}else{
		        	  mainCanvas.room=new RoomCanvas;
		        	  mainCanvas.room.mainCanvas=mainCanvas;	
		              mainCanvas.room.x=9;
		              mainCanvas.room.y=13;
		        	  mainCanvas.gameForm.addChild(mainCanvas.room);
		        	  mainCanvas.room.initSeats();	
		        	  mainCanvas.room.initRoom();		
	        	 	}
	        	}else
	        	if(status=="shop"){
	              mainCanvas.shop=new ShopCanvas;
	              mainCanvas.shop.x=0;
	              mainCanvas.shop.y=0;
	        	  mainCanvas.adcanvas.visible=false;
	        	  mainCanvas.boardcanvas.visible=false;
	        	  mainCanvas.uicanvas.visible=false;
	               mainCanvas.chatcanvas.alpha=1;
	        	  mainCanvas.gameForm.addChild(mainCanvas.shop);
	        	   mainCanvas.shop.mainCanvas=mainCanvas;
	        	}else
	        	if(status=="tyroroom"){
	              mainCanvas.tyroroom=new TyroRacingRoom;
	              mainCanvas.tyroroom.x=0;
	              mainCanvas.tyroroom.y=0;
	        	  mainCanvas.adcanvas.visible=false;
	        	  mainCanvas.boardcanvas.visible=false;
	        	  mainCanvas.uicanvas.visible=false;
	              mainCanvas.chatcanvas.alpha=0.2;
	        	  mainCanvas.gameForm.addChild(mainCanvas.tyroroom);
	        	  mainCanvas.tyroroom.mainCanvas=mainCanvas;
	        	  this.removeFuncBtn();
			   	  mainCanvas.tyroroom.initRoom(mainCanvas);
			   	  mainCanvas.clearAll();
	        	}
	        	
	        }
	        
	        
	}
}